package com.cennavi.mineplay.mineplay_data.redis;

import java.io.Serializable;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;

import com.cennavi.mineplay.mineplay_data.bean.RedisConfigBean;
import com.navinfo.mapspotter.foundation.io.util.DataSourceParams;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolSet implements Serializable {
    private static final long serialVersionUID = 1L;
    private DataSourceParams params;
    private static JedisPool jedisPool;
    private static ReentrantLock lock = new ReentrantLock();
    public static String host="";
    @Autowired
    private static RedisConfigBean configBean;
    public JedisPoolSet(DataSourceParams params) {
        super();
        this.params = params;
    }

    private static class MySingletonHandler {
        private static JedisPool instance;
        private static JedisPoolConfig config = new JedisPoolConfig();

       
        static {
            config.setMaxTotal(200);
            config.setMaxIdle(50);
            config.setMinIdle(20);
            config.setMaxWaitMillis(60000);
            config.setTestOnBorrow(false);
            config.setTimeBetweenEvictionRunsMillis(30000);
            config.setTestWhileIdle(true);
            config.setNumTestsPerEvictionRun(20);
            config.setMinEvictableIdleTimeMillis(60000);
            instance = new JedisPool(config, configBean.getHost(), configBean.getPort(), 10000);
        }
    }


    public static JedisPool getInstance(RedisConfigBean configBean) {
    	JedisPoolSet.configBean=configBean;
        return MySingletonHandler.instance;
    }

    public static JedisPool getInstance(String host, int port) {
        if (jedisPool == null) {
            lock.lock();
            if (jedisPool == null) {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(128);
                config.setMinIdle(10);
                config.setMaxIdle(30);
                config.setMaxWaitMillis(30000l);
                config.setTestWhileIdle(false);
                config.setTestOnBorrow(false);
                jedisPool = new JedisPool(config, host, port, 100000);
            }
            lock.unlock();
        }
        return jedisPool;
    }



}
